package com.example.hotel_lxy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.bean.ReserveInfoBean;
import com.example.hotel_lxy.ui.activity.SetUpReserveActivity;
import com.example.hotel_lxy.ui.dialog.ImageDialog;
import com.example.hotel_lxy.utils.DatePhotoUtils;
import com.example.hotel_lxy.utils.DateUtils;
import com.example.hotel_lxy.utils.JurisdictionUtils;

import java.util.List;


public class ReserveInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //两个final分别代表两个不同的布局
    public static final int ITEMONE = 1;
    public static final int ITEMTWO = 2;

    //上下文
    private Context context;
    private List<ReserveInfoBean.DataBeanX.ScheduledDepositBean>  newArrsBean;


    /**
     * 构造方法
     *
     * @param context
     * @param
     * @param
     */
    public ReserveInfoAdapter(Context context, List<ReserveInfoBean.DataBeanX.ScheduledDepositBean>  newArrsBean) {
        this.context = context;
        this.newArrsBean=newArrsBean;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //这时候就要根据这个i来判断加哪一个布局了
        View inflate = null;
        RecyclerView.ViewHolder viewHolder = null;
        //根据i返回不同布局
        switch (i) {
            case ITEMONE:
                inflate = LayoutInflater.from(context).inflate(R.layout.item_reserve_info_one, viewGroup, false);
                viewHolder = new OneItemHolder(inflate);
                break;
            case ITEMTWO:
                inflate = LayoutInflater.from(context).inflate(R.layout.item_reserve_info_two, viewGroup, false);
                viewHolder = new TwoItemHolder(inflate);
                break;
        }
        //返回布局
        return viewHolder;
    }

    /**
     * 绑定控件，这里可以写一些事件方法等
     *
     * @param viewHolder
     * @param i
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        //如果当前的 viewHolder 属于 OneItemHolder 则执行
        if (viewHolder instanceof OneItemHolder) {
            if(newArrsBean.size()==1){
                ((OneItemHolder) viewHolder).view1.setVisibility(View.GONE);
            }
            Glide.with(context).load(newArrsBean.get(i).getImage()).into(  ((OneItemHolder) viewHolder).img);
            ((OneItemHolder) viewHolder).text_money.setText(""+newArrsBean.get(i).getMoney()+""+newArrsBean.get(i).getType_name());
            if (!JurisdictionUtils.getReceiptStatus(context)) {
                ((OneItemHolder) viewHolder).text_num.setVisibility(View.GONE);
            }else{
                ((OneItemHolder) viewHolder).text_num.setText("收据编号："+newArrsBean.get(i).getNum());
            }

            ((OneItemHolder) viewHolder).text_type.setText(""+newArrsBean.get(i).getPay_name());
            String dateHh = DateUtils.getDateHh(Long.valueOf(newArrsBean.get(i).getAddtime()));
            ((OneItemHolder) viewHolder).text_time.setText(dateHh+"");
           int j=i+1;
            String d = DatePhotoUtils.numberToChinese(j);
            if(d.equals("一")){
                ((OneItemHolder) viewHolder).text_title.setText("首次确认方式");
            }else{
                ((OneItemHolder) viewHolder).text_title.setText(""+d+"次确认方式");
            }
            int finalI = i;
            ((OneItemHolder) viewHolder).img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImageDialog.show(context,null,newArrsBean.get(finalI).getImage());
                }
            });
        }
        else if (viewHolder instanceof TwoItemHolder) {
            if(newArrsBean.size()==1){
                ((TwoItemHolder) viewHolder).view1.setVisibility(View.GONE);
            }
            ((TwoItemHolder) viewHolder).text_type.setText(""+newArrsBean.get(i).getG_name());
            String dateHh = DateUtils.getDateHh(Long.valueOf(newArrsBean.get(i).getAddtime()));
            ((TwoItemHolder) viewHolder).text_time.setText(""+dateHh);
            i=i+1;
            String d = DatePhotoUtils.numberToChinese(i);
            if(d.equals("一")){
                ((TwoItemHolder) viewHolder).text_title.setText("首次确认方式");
            }else{
                ((TwoItemHolder) viewHolder).text_title.setText(""+d+"次确认方式");
            }
        }
    }

    public interface OnDeleteListener{
        void delete();
    }
    //声明接口对象
    private OnDeleteListener mOnDeleteListener;

    //设置监听器,实例化接口
    public void setOnDeleteListener(OnDeleteListener onDeleteListener){
        mOnDeleteListener = onDeleteListener;
    }

    /**
     * 返回条目总数量，假设16个条目
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return newArrsBean.size();
    }

    /**
     * 返回条目类型(这里就做个简单的判断区分)
     *
     * @param position 代表第几个条目
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if(newArrsBean.get(position).getType()==1){
            return ITEMONE;
        }else {
            return ITEMTWO;
        }
    }

    /**
     * 第一个布局的Holder，要继承自RecyclerView.ViewHolder，这里你可以绑定控件
     */
    class OneItemHolder extends RecyclerView.ViewHolder {
        TextView text_title;
        TextView text_money;
        TextView text_num;
        TextView text_type;
        TextView text_time;
        ImageView img;
        View view1;
        public OneItemHolder(@NonNull View itemView) {
            super(itemView);
            text_title=itemView.findViewById(R.id.text_title);
            text_money=itemView.findViewById(R.id.text_money);
            text_num=itemView.findViewById(R.id.text_num);
            text_type=itemView.findViewById(R.id.text_type);
            text_time=itemView.findViewById(R.id.text_time);
            img=itemView.findViewById(R.id.img);
            view1=itemView.findViewById(R.id.view1);
        }
    }

    /**
     * 第二个布局的Holder，要继承自RecyclerView.ViewHolder，这里你可以绑定控件
     */
    class TwoItemHolder extends RecyclerView.ViewHolder {
        TextView text_title;
        TextView text_type;
        TextView text_time;
        View view1;
        public TwoItemHolder(@NonNull View itemView) {
            super(itemView);
            text_title=itemView.findViewById(R.id.text_title);
            text_type=itemView.findViewById(R.id.text_type);
            text_time=itemView.findViewById(R.id.text_time);
            view1=itemView.findViewById(R.id.view1);
        }
    }
}
